package cn.com.huzo.webapp.controller.trade;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.trade.MasterProductionScheduleHeaderEntity;
import cn.com.huzo.model.entities.trade.MasterProductionScheduleLinesEntity;
import cn.com.huzo.model.entities.trade.MasterProductionScheduleLinesTrackEntity;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.PathVariable;
import cn.com.huzo.service.trade.MasterProductionScheduleHeaderManager;
import cn.com.huzo.service.trade.MasterProductionScheduleLinesManager;

/**
 * 产品计划控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/trade/master_production_schedule")
public class MasterProductionScheduleController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    private final String _taskUrl = "/list.html";

    @Resource(name = "MasterProductionScheduleHeaderManager")
    private MasterProductionScheduleHeaderManager _manager;

    @Resource(name = "MasterProductionScheduleLinesManager")
    private MasterProductionScheduleLinesManager _manager_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到产品计划一览画面
     * <p>
     * @param request
     * @return
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("trade.master_production_schedule.list.def");
        return view;
    }

    /**
     * 产品计划编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作标志 1;增加 2：编辑
     * @param mpsId 产品计划标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/{action}/{mpsId}/edit.html",
            method = {RequestMethod.GET})
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "mpsId") Long mpsId) throws Exception {
        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("mpsId", mpsId.toString());

        view.setViewName("trade.master_production_schedule.edit.def");
        return view;
    }

    /**
     * 弹出产品计划选择画面
     * <p>
     * @return
     */
    @RequestMapping("/choice.html")
    public ModelAndView redirectHeaderPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.trade.master_production_schedule.choice.def");
        return view;
    }

    /**
     * 弹出产品计划-明细选择画面
     * <p>
     * @return
     */
    @RequestMapping("/lines/choice.html")
    public ModelAndView redirectLinesPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.trade.master_production_schedule_lines.choice.def");
        return view;
    }

    /**
     * 同步更新计划跟踪项目画面
     * <p>
     * @return @throws java.lang.Exception
     */
    @RequestMapping("/lines/track_item/edit.html")
    public ModelAndView redirectSyncLinesTrackItemDatePopupWnd() throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.trade.master_production_schedule_lines_trackitem.edit.def");
        return view;
    }

    /**
     * 产品计划-打印预览画面
     * <p>
     * @return
     */
    @RequestMapping("/print.html")
    public ModelAndView redirectPrintPreviewWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("print.trade.master_production_schedule.print.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 根据标识取得产品计划数据。
     *
     * @param criterion 产品标识、当前用户、流程标识
     * @return 产品计划数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchByHeaderKey(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "CurrentUserId"),
                MapUtils.getLong(criterion, "WorkFlowId"));
    }

    /**
     * 删除产品计划数据。
     *
     * @param criterion 产品计划标识
     * @return 返回删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }

    /**
     * 根据条件取得产品计划分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 产品计划分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        criterion.put("TaskUrl", _taskUrl);
        return _manager.fetchForPage(criterion);
    }

    /**
     * 保存产品计划数据。
     * <p>
     * @param instance 要保存的产品计划对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody MasterProductionScheduleHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { //编辑
            rowsAffected = (long) _manager.update(instance);
        } else {//新增
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 设置完成标记。
     * <p>
     * @param criterion 条件
     * @return 影响行数
     * @throws Exception
     */
    @RequestMapping(value = "/setOverFlag.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer setOverFlag(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.setOverFlag(MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getString(criterion, "OverFlag"));
    }

    /**
     * 审核产品计划数据。
     * <p>
     * @param criterion 要审核产品计划对象。
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "/check.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> check(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.doCheck(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "WorkFlowId"),
                MapUtils.getShort(criterion, "NodeOrder"),
                MapUtils.getLong(criterion, "CheckedBy"),
                MapUtils.getShort(criterion, "IsCheckCurrentNode")
        );
    }

    /**
     * 反审产品计划数据。
     * <p>
     * @param criterion 要审核产品计划对象。
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "/uncheck.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> unCheck(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.doUnCheck(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "WorkFlowId"),
                MapUtils.getShort(criterion, "NodeOrder"),
                MapUtils.getLong(criterion, "CheckedBy")
        );
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细-数据处理部分">
    /**
     * 根据主表标识，取得明细表数据。
     *
     * @param headerKey
     * @return 明细列表数据
     * @throws Exception
     */
    @RequestMapping(value = "/lines/fetchByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<MasterProductionScheduleLinesEntity> fetchLinesByHeaderKey(@RequestBody Long headerKey) throws Exception {
        return _manager_lines.fetchLinesByHeaderKey(headerKey);
    }

    /**
     * 增加产品计划计划明细数据。
     *
     * @param instance 产品标识
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> insertLines(@RequestBody List<MasterProductionScheduleLinesEntity> instance) throws Exception {
        return _manager_lines.insertLines(instance);
    }

    /**
     * 更新产品计划计划明细数据。
     *
     * @param itemsList 要保存的数据
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer updateLines(@RequestBody List<MasterProductionScheduleLinesEntity> itemsList) throws Exception {
        return _manager_lines.updateLines(itemsList);
    }

    /**
     * 同步更新产品计划计划明细日期数据。
     *
     * @param itemList 要保存的数据
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/syncupdate.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<MasterProductionScheduleLinesEntity> updateLinesForSync(@RequestBody List<MasterProductionScheduleLinesTrackEntity> itemList) throws Exception {
        if (itemList == null || itemList.isEmpty()) {
            return new ArrayList();
        } else {
            return _manager_lines.syncUpdateTrackItem(itemList);
        }
    }

    /**
     * 删除产品计划计划明细数据。
     *
     * @param criterion 要删除的明细标识、主表id、删除的明细信息等
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }
    // </editor-fold>
}
